package com.kj.xuyuan.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kj.xuyuan.R;
import com.kj.xuyuan.base.BaseActivity;
import com.kj.xuyuan.base.BaseModel;
import com.kj.xuyuan.base.MyApp;
import com.kj.xuyuan.http.JsonCallback;
import com.kj.xuyuan.http.JsonNoEncryptionCallback;
import com.kj.xuyuan.http.OkGoUtil;
import com.kj.xuyuan.http.UrlConstants;
import com.kj.xuyuan.model.UploadBean;
import com.kj.xuyuan.pop.UniversalClickPopWindow;
import com.kj.xuyuan.util.GlideUtils;
import com.kj.xuyuan.util.LogUtils;
import com.kj.xuyuan.util.PhotoSelectSingleUtile;
import com.kj.xuyuan.util.ToastUtil;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.model.Response;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RealPersonAuthActivity extends BaseActivity {

    @BindView(R.id.rl_image)
    RelativeLayout rlImage;
    @BindView(R.id.tv_auth)
    TextView tvAuth;
    @BindView(R.id.iv_head)
    ImageView ivHead;

    private List<LocalMedia> mSelectList = new ArrayList<>();
    private LocalMedia image;
    private String avatar;

    public static void actionStar(Context context) {
        Intent intent = new Intent(context, RealPersonAuthActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_real_person_auth;
    }

    @Override
    protected void initView() {
        setTitleText("真人认证");
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return 0;
    }

    @OnClick({R.id.rl_image, R.id.tv_auth})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_image:
                popGetImageMethodWindow();
                break;
            case R.id.tv_auth:
                auth();
                break;
        }
    }

    private void auth() {
        Map map = new HashMap();
        map.put("type", "1");
        map.put("image", avatar);

        OkGoUtil.postRequest(getContext(), UrlConstants.AUTH, map, new JsonCallback<BaseModel<?>>() {
            @Override
            public void onSuccess(Response<BaseModel<?>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        finish();
                    }
                    ToastUtil.showToast(getContext(),response.body().getMsg());
                }
            }

            @Override
            public void onError(Response<BaseModel<?>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
            }
        });
    }

    private void popGetImageMethodWindow() {
        List<String> data = new ArrayList<>();
        data.add("拍摄");
        data.add("从相册选择");
        UniversalClickPopWindow popWindow = new UniversalClickPopWindow(context,data);
        popWindow.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM,0,0);
        popWindow.setAnimationStyle(R.style.popwindow_center_anim_style);
        popWindow.setItemClickListener(new UniversalClickPopWindow.ItemClickListener() {
            @Override
            public void click(int position) {
                if(position == 0)
                    PhotoSelectSingleUtile.cameraPhoto(context, PictureMimeType.ofImage());
                else
                    PhotoSelectSingleUtile.selectPhoto(context, PictureMimeType.ofImage(),mSelectList, 1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择结果回调
                    List<LocalMedia> localMedia = PictureSelector.obtainMultipleResult(data);
                    if(localMedia.size() == 0){
                        return;
                    }
                    mSelectList.clear();
                    image = localMedia.get(0);
                    uploadImage();
                    break;
            }
        }
    }

    private void uploadImage() {
        showDialog();
        File file = new File(MyApp.selectPhotoShow(context,image));
        OkGoUtil.postSingleFile(context, UrlConstants.UPLOAD,file,new JsonNoEncryptionCallback<BaseModel<UploadBean>>() {
            @Override
            public void onSuccess(Response<BaseModel<UploadBean>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        avatar = response.body().getData().getUrl();
                        GlideUtils.loadImage(context,UrlConstants.Base_Http+ avatar,ivHead);
                    }
                    ToastUtil.showToast(context,response.body().getMsg());
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<BaseModel<UploadBean>> response) {
                super.onError(response);
                dismissDialog();
                LogUtils.e(TAG,"response ="+response.getException());
            }
        });
    }
}